<template>
  <view>
    <view class="goods-item" @click="gotodetail($event)">
      <!-- 商品左侧图片区域 -->
      <view class="goods-item-left">
        <radio style="padding: 0 7px;" color="#ff5c00" data-value="1" :checked="goods.goods_status" v-if="showRadio" @click="radioClick"></radio>
        <image :src="goods.goods_small_logo || defaultPic" mode="" class="goods-pic"></image>
      </view>
      <!-- 商品右侧信息区域 -->
      <view class="goods-item-right">
        <!-- 商品标题 -->
        <view class="goods-name">{{goods.goods_name}}</view>
        <view class="goods-info">
          <!-- 商品信息价格 -->
          <view class="goods-price">￥{{goods.goods_price | tofixed}}</view>
          <view class="number" v-show="!showNumBox" data-value="1" @click="shownum" v-if="showNum">
           x {{goods.goods_count}}
          </view>
          <uni-number-box @change="numChange" v-show="showNumBox" data-value="1"  :min="minNum" :value="goods.goods_count" v-if="showNum"></uni-number-box>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    name:"my-goods",
    props:{
      goods:{
        type:Object,
        default:{}
      },
      showRadio:{
        type:Boolean,
        // 默认情况下，不会展示radio组件
        default:false
      },
      showNum:{
        type:Boolean,
        // 默认情况下，不会展示numberbox组件
        default:false
      }
    },
    data() {
      return {
        // 默认图片
        defaultPic:'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
        showNumBox:false,
        minNum:1
      }
    },
    filters:{
      tofixed(num){
        return Number(num).toFixed(2)
      }
    },
    methods:{
      gotodetail(e){
        if(e.mp.target.dataset.value === '1'){
          return
        }
        this.$emit('go-detail')
      },
      // 单选框点击事e件
      radioClick(){
        this.$emit('radio-change',{
          goods_id:this.goods.goods_id,
          goods_status:!this.goods.goods_status
        })
      },
      // 展示隐藏numberbox按钮
      shownum(){
        this.showNumBox = true
      },
      // 监听numberBox组件的值  
      numChange(val){
        this.$emit('num-change',{
          goods_id:this.goods.goods_id,
          goods_count:+val
        })
      }
    }
  }
</script>

<style lang="scss">
.goods-item{
    display: flex;
    padding: 10px 5px;
    border-bottom: 1px solid #f0f0f0;
    .goods-item-left{
      margin-right: 10px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .goods-pic{
        width: 100px;
        height: 100px;
        display: block;
      }
    }
    .goods-item-right{
      display: flex;
      flex: 1;
      flex-direction: column;
      justify-content: space-between;
      padding-right: 7px;
      .goods-name{
        font-size: 13px;
      }
      .goods-info{
        display: flex;
        justify-content: space-between;
        align-items: center;
        .goods-price{
          color: #c00000;
          font-size: 16px;
        }
      }
    }
  }
  .number{
    display: flex;
    justify-content: center;
    align-items: center;
    // width: 20px;
    // height: 20px;
    padding:5px;
    border-radius: 5px;
    background-color: #f5f5f5;
  }
</style>